package cn.kgc.itrip.search.service.impl;

import cn.kgc.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.kgc.itrip.beans.vo.hotel.SearchHotelVO;
import cn.kgc.itrip.search.dao.SearchHotelDao;
import cn.kgc.itrip.search.entity.HotelSearchVO;
import cn.kgc.itrip.search.service.SearchHotelService;
import cn.kgc.itrip.utils.EmptyUtils;
import cn.kgc.itrip.utils.Page;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchHotelServiceImpl implements SearchHotelService {

    //因为是连接的solr 所以dao是我们自己写的
    @Resource
    private SearchHotelDao searchHotelDao;

    @Override
    public void searchHotelListForPaging(Page<HotelSearchVO> page, SearchHotelVO vo) throws Exception {
    //结合solr

        //初始化查询条件 设定目的地
        SolrQuery solrQuery = new SolrQuery("destination:"+vo.getDestination());


        //关键字
        if(EmptyUtils.isNotEmpty(vo.getKeywords())&& !"null".equals(vo.getKeywords())){
            solrQuery.addFilterQuery("keyword"+vo.getKeywords());
        }

        //价格(此范围筛选是按照最低价格进行筛选的)
        if(EmptyUtils.isNotEmpty(vo.getMinPrice()) && EmptyUtils.isNotEmpty(vo.getMaxPrice())){
            String format = String.format("minPrice:[%s TO %s]", vo.getMinPrice(), vo.getMaxPrice());
            solrQuery.addFilterQuery(format);
        }

        //星级
        if(EmptyUtils.isNotEmpty(vo.getHotelLevel())){
            solrQuery.addFilterQuery("hotelLevel:"+vo.getHotelLevel());
        }

        //  "featureIds": ",118,115,17,",   Solr存储的信息
        if(EmptyUtils.isNotEmpty(vo.getFeatureIds())) {
            // featureIds: 117,17    或   featureIds: 117  前端传输
            // 选择的字段越多，则要求越多 数据越来越少
            // 将前端传输的字符串进行切割
            String[] featureIds = vo.getFeatureIds().split(",");
            StringBuffer featureIdBuffer=new StringBuffer();
            if(featureIds.length>1){
                //说明里面有好几个
                // featureIds：*,117,*  AND featureIds：*,17,*
                for(int i=0;i<featureIds.length;i++){
                    if(i==0){
                        featureIdBuffer.append("featureIds:*,"+featureIds[0]+"*, ");
                    }else{
                        featureIdBuffer.append(" AND featureIds:*,"+featureIds[i]+"*,");
                    }
                }

            }else{
                // featureIds：*,117,* 之所以前后加上,号 是因为防止 117 包含 *17*  这是不合理的
                featureIdBuffer.append("featureIds:*,"+featureIds[0]+"*,");
            }

            solrQuery.setQuery(featureIdBuffer.toString());
        }

        // 排序
        // 最受欢迎   评分  价格  星级
        // ascSort:isOkCount
        if(EmptyUtils.isNotEmpty(vo.getAscSort())){
            solrQuery.setSort(vo.getAscSort(), SolrQuery.ORDER.asc);
        }

        // descSort:isOkCount
        if(EmptyUtils.isNotEmpty(vo.getDescSort())){
            solrQuery.setSort(vo.getDescSort(), SolrQuery.ORDER.desc);
        }

        long count = searchHotelDao.getHotelCount(solrQuery);
        page.setTotal((int) count);

        //分页
        solrQuery.setStart(page.getBeginPos());
        solrQuery.setRows(page.getPageSize());
        List<HotelSearchVO> list = searchHotelDao.getHotelListForPaging(solrQuery);
        page.setRows(list);
    }

    @Override
    public List<HotelSearchVO> searchHotelListByHotCity(SearchHotCityVO vo) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("cityId",vo.getCityId());
        map.put("count",vo.getCount());
        return searchHotelDao.getHotelListByParams(map);

    }

//测试字符串的拆分
    public static void main(String[] args) {
        String str = "17";
        String[] split = str.split(",");
        System.out.println(split[0]);
    }
}




